package solverStrategies;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeMap;

import algo.ConvertStructToArray;
import sudoku.SudokuEinzelfeld;
import sudoku.SudokuSpielfeld;

public class BasicReduce implements SolverAlgorithm {

	private final int LEVEL = 0;

	@Override
	public boolean manipulateSudoku(SudokuSpielfeld sf) {
		TreeMap<Integer, Set<Integer>> possibleNumbers = new TreeMap<Integer, Set<Integer>>();
		ConvertStructToArray conv = new ConvertStructToArray(sf);
		boolean changes = false;
		for (int rows = 0; rows < sf.getFIELDLENGTH(); rows++) {
			for (int idx = 0; idx < sf.getFIELDLENGTH(); idx++) {
				SudokuEinzelfeld field = sf.returnEinzelnesFeld(rows, idx);
				if (field.getNumber() == 0) {
					Set<Integer> possNrs = field.getPossibleNumbers();
					Set<Integer> newPossNrs = conv
							.returnPossibleNumbersWithLookUpToOtherStructs(field);
					if (!possNrs.isEmpty()) {
						if (possNrs.size() < newPossNrs.size()) {
							newPossNrs = possNrs;
						}
					}

					possibleNumbers.put((rows * 10) + idx, newPossNrs);
					boolean changesTmp = false;
					for (int i = 1; i <= 9; i++) {
						if (!newPossNrs.contains(i)) {
							changesTmp = field.removeNumberFromPossibleNumbers(i);
							if(changesTmp){
								changes = true;
							}
						}
					}

				} else {
					possibleNumbers.put((rows * 10) + idx,
							new HashSet<Integer>());
				}

			}
		}

		return changes;
	}

	@Override
	public int returnLevel() {
		return LEVEL;
	}

}
